/*
 * OpenFrame.java
 *
 * Created on 1. Juni 2008, 13:11
 */

package twi_project;

import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;

import jtools.files.TextFile;
import jtools.files.filesystem.FileDialog;

/**
 * 
 * @author Soeren
 */
public class OpenFrame extends javax.swing.JFrame {

	private static File recentOntologieFile = new File(
			"data/db/recentFiles.txt");
	private static File autoloadOntologieFile = new File("data/db/autoLoad.txt");

	public OpenFrame() {
		this(false);
	}

	/** Creates new form OpenFrame */
	public OpenFrame(boolean forceDialog) {
		initComponents();
		this.jLabel2.setText("");
		loadList();
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		if (!forceDialog && autoloadOntologieFile.exists()) {
			TextFile tf = new TextFile();
			try {
				tf.readFile(autoloadOntologieFile, false);
				File f = new File(tf.getNextLine());
				tf.closeFile();
				if (!f.exists()) {
					this.jLabel2
							.setText("Could not find File, please choose another one");
					setVisible(true);
				} else {
					TWI.getInstance().setOntologieFile(f);
					setVisible(false);
					dispose();
					TWI.getInstance().start();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else
			setVisible(true);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated
	// Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jButton_open = new javax.swing.JButton();
		jButton_browse = new javax.swing.JButton();
		jButton_close = new javax.swing.JButton();
		jCheckBox1 = new javax.swing.JCheckBox();
		jLabel2 = new javax.swing.JLabel();
		list1 = new java.awt.List();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setName("Form"); // NOI18N

		org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application
				.getInstance(twi_project.TWI_ProjectApp.class).getContext()
				.getResourceMap(OpenFrame.class);
		jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
		jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
		jLabel1.setName("jLabel1"); // NOI18N

		jButton_open.setText(resourceMap.getString("jButton_open.text")); // NOI18N
		jButton_open.setName("jButton_open"); // NOI18N
		jButton_open.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton_openActionPerformed(evt);
			}
		});

		jButton_browse.setText(resourceMap.getString("jButton_browse.text")); // NOI18N
		jButton_browse.setName("jButton_browse"); // NOI18N
		jButton_browse.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton_browseActionPerformed(evt);
			}
		});

		jButton_close.setText(resourceMap.getString("jButton_close.text")); // NOI18N
		jButton_close.setName("jButton_close"); // NOI18N
		jButton_close.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton_closeActionPerformed(evt);
			}
		});

		jCheckBox1.setText(resourceMap.getString("jCheckBox1.text")); // NOI18N
		jCheckBox1.setName("jCheckBox1"); // NOI18N

		jLabel2.setForeground(resourceMap.getColor("jLabel2.foreground")); // NOI18N
		jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
		jLabel2.setName("jLabel2"); // NOI18N

		list1.setName("list1"); // NOI18N

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				114,
																				114,
																				114)
																		.addComponent(
																				jLabel1))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jCheckBox1,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								290,
																								Short.MAX_VALUE)
																						.addComponent(
																								list1,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								290,
																								Short.MAX_VALUE))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jButton_open,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								85,
																								Short.MAX_VALUE)
																						.addComponent(
																								jButton_browse,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								85,
																								Short.MAX_VALUE)
																						.addComponent(
																								jButton_close,
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				jLabel2,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				385,
																				Short.MAX_VALUE)))
										.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(jLabel1)
										.addGap(9, 9, 9)
										.addComponent(jLabel2)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jButton_open)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																				22,
																				Short.MAX_VALUE)
																		.addComponent(
																				jButton_browse)
																		.addGap(
																				33,
																				33,
																				33))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				list1,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				91,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jButton_close)
														.addComponent(
																jCheckBox1))
										.addContainerGap()));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void jButton_openActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_openActionPerformed
		String path = list1.getSelectedItem();
		if (path == null) {
			this.jLabel2.setText("Please choose a File to open");
			return;
		}
		File f = new File(path);
		if (!f.exists()) {
			this.jLabel2.setText("Could not find File");
			return;
		}
		if (this.jCheckBox1.isSelected()) {
			TextFile tf = new TextFile();
			try {
				tf.add(path);
				tf.writeFile(autoloadOntologieFile, true);
				tf.closeFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		setVisible(false);
		dispose();
		TWI.getInstance().setOntologieFile(f);
		TWI.getInstance().start();
	}// GEN-LAST:event_jButton_openActionPerformed

	private void jButton_browseActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_browseActionPerformed
		File file = FileDialog.openDialog(this, new String[] { ".owl" },
				"Ontologie Dateien", false);
		if (file == null)
			return;
		this.list1.addItem(file.getAbsolutePath());
		saveList(file);
		setVisible(false);
		dispose();
		TWI.getInstance().setOntologieFile(file);
		TWI.getInstance().start();
	}

	private void loadList() {
		if (!recentOntologieFile.exists())
			return;
		TextFile tf = new TextFile();
		try {
			tf.readFile(recentOntologieFile, false);
			while (tf.hasNext())
				list1.addItem(tf.getNextLine());
			tf.closeFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void saveList(File file) {
		if (!this.recentOntologieFile.getParentFile().exists())
			this.recentOntologieFile.getParentFile().mkdirs();
		TextFile tf = new TextFile();
		try {
			if (recentOntologieFile.exists()) {
				TextFile tf1 = new TextFile();
				tf1.readFile(recentOntologieFile, true);
				while (tf1.hasNext())
					tf.add(tf1.getNextLine());
				tf1.closeFile();
			}
			tf.add(file.getAbsolutePath());
			tf.writeFile(recentOntologieFile, true);
			tf.closeFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void jButton_closeActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_closeActionPerformed
		if (TWI.getInstance().getOntologieFile() == null)
			System.exit(0);
		setVisible(false);
		dispose();
	}// GEN-LAST:event_jButton_closeActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new OpenFrame().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton jButton_browse;
	private javax.swing.JButton jButton_close;
	private javax.swing.JButton jButton_open;
	private javax.swing.JCheckBox jCheckBox1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private java.awt.List list1;
	// End of variables declaration//GEN-END:variables

}
